package com.cn.jdia.di.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBManager4Oracle {

	public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
	public static final String USER = "qdjc_dm";
	public static final String PASS = "qdjc_dm";
	public static final String URL = "jdbc:oracle:thin:@172.20.5.76:1521/qdjcdw";

	private static DBManager4Oracle per = null;

	private Connection conn = null;
	private Statement stmt = null;

	public DBManager4Oracle() {

	}

	public static DBManager4Oracle createInstance() {
		if (per == null) {
			per = new DBManager4Oracle();
			per.initDB();
		}
		return per;
	}

	public void initDB() {
		try {
			Class.forName(DRIVER);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void connectDB() {
		System.out.println("Connecting to database...");
		try {
			conn = DriverManager.getConnection(URL, USER, PASS);
			stmt = conn.createStatement();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("SqlManager:Connect to database successful.");
	}

	public void closeDB() {
		System.out.println("Close connection to database..");
		try {
			if (stmt != null) {
				stmt.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("Close connection successful");
	}

	public ResultSet executeQuery(String sql) {
		ResultSet rs = null;
		Statement stmt = null;

		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);

		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return rs;
	}

	public int executeUpdate(String sql) {
		int ret = 0;
		try {
			ret = stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ret;
	}
}
